package com.csist.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.csist.pojo.bo.SpecificationClassInfoBo;
import com.csist.pojo.po.basicInfoModule.EducationStatus;
import com.csist.service.bo.SpecificationClassInfoBoService;
import com.csist.service.bo.impl.SpecificationClassInfoBoServiceImpl;
import com.csist.util.JSONUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/specificationClassInfo")
public class SpecificationClassInfoServlet extends HttpServlet {
    private JSONObject jsonObject;
    private SpecificationClassInfoBoService specificationClassInfoServlet = new SpecificationClassInfoBoServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        //        这里如果request没有json字符串，则jsonObject为null（获取不到）
        jsonObject = JSONUtil.getJSONObject(request);
        if (jsonObject == null) {
            err(request, response);
            return;
        }
        String type = jsonObject.getString("type");
        if (type != null) {
            switch (type) {
                case "add":
                    add(request, response);
                    break;
                case "query":
                    show(request, response);
                    break;
                default:
                    err(request, response);
            }
        } else {
            err(request, response);
        }
    }

    private void add(HttpServletRequest request, HttpServletResponse response) {

    }

    private void show(HttpServletRequest request, HttpServletResponse response) throws IOException {

//        当jsonObject为空时
        if (jsonObject == null) {
            err(request, response);
            return;
        }

        //        存储数据、是否执行成功、判断结果
        HashMap<String, Object> map = new HashMap<>();

        Integer pageNum = jsonObject.getInteger("pageNum");
        Integer num = pageNum == null ? 1 : pageNum;

        //        获取xml中的每页条数导航页数
        String pageSize = this.getServletContext().getInitParameter("pageSize");
        String navigatePages = this.getServletContext().getInitParameter("navigatePages");
        //开启分页，当前页码为num每页显示pageSize条数据
        PageHelper.startPage(num, Integer.parseInt(pageSize));
        List<SpecificationClassInfoBo> list = specificationClassInfoServlet.selectAllSpecificationClassInfo();
        if (list != null) {
            PageInfo<SpecificationClassInfoBo> pageInfo = new PageInfo<>(list, Integer.parseInt(navigatePages));
            map.put("pagination", pageInfo);
            map.put("status", true);
            map.put("msg", "查询信息成功...");
            response.getWriter().write(JSON.toJSONString(map));
            return;
        }
        map.put("pagination", null);
        map.put("status", false);
        map.put("msg", "无匹配数据...");
        response.getWriter().write(JSON.toJSONString(map));

    }

    private void err(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("status", false);
        map.put("msg", "传送了无效数据...");
        response.getWriter().write(JSON.toJSONString(map));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
